package com.bjut.vendormgrsys.web.rest;

import com.bjut.vendormgrsys.constant.Constant;
import com.bjut.vendormgrsys.service.SystemOrganizationService;
import com.bjut.vendormgrsys.support.bo.UserBO;
import com.bjut.vendormgrsys.util.DateUtils;
import com.bjut.vendormgrsys.web.dto.*;
import com.bjut.vendormgrsys.web.dto.systemOrganization.SystemOrganizationInfo;
import com.bjut.vendormgrsys.web.dto.systemOrganization.SystemOrganizationSearch;
import com.bjut.vendormgrsys.web.dto.user.Body;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SystemOrganizationApiController implements SystemOrganizationApi{

    private final SystemOrganizationService systemOrganizationService;
    private final HttpServletRequest request;
    private static final Logger log = LoggerFactory.getLogger(UserApiController.class);

    @Autowired
    public SystemOrganizationApiController(SystemOrganizationService systemOrganizationService, HttpServletRequest request){
        this.systemOrganizationService = systemOrganizationService;
        this.request = request;
    }

    @Override
    public ResponseEntity<Success> systemOrganizationCreatePost(@Valid SystemOrganizationInfo body) {
        ResponseEntity<Success> successEntity;
        try{
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO creator = (UserBO) authentication.getPrincipal();

            String id = body.getId();
            if (!StringUtils.hasLength(id)) {
                body.setStatus(Constant.NewsStatus.NEW.getKey());
                body.setCreatorId(creator.getId());
                body.setDateCreated(DateUtils.now());
                body.setLastUpdated(DateUtils.now());
            }

            systemOrganizationService.saveNews(body);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.OK.value()).message("保存成功"), HttpStatus.OK);
        }catch (Exception e){
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("保存失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return successEntity;
    }

    @Override
    public ResponseEntity<ResultList> systemOrganizationFindAllPost(@Valid SystemOrganizationSearch body) {
        ResponseEntity<ResultList> successEntity;

        try{
            List<SystemOrganizationInfo> list = new ArrayList<>();
            long totalCount = systemOrganizationService.findAll(body, list);
            successEntity = new ResponseEntity<>(new ResultList().code(HttpStatus.OK.value()).message("成功").data(new ResultListData().totalCount(totalCount).items(list)), HttpStatus.OK);
        }catch (Exception e){
            successEntity = new ResponseEntity<>(new ResultList().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("获取列表失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return successEntity;
    }

    @Override
    public ResponseEntity<Success> changeStatusPost(@Valid List<Body> body) {
        ResponseEntity<Success> successEntity;
        try {
            systemOrganizationService.changeStatus(body);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.OK.value()).message("修改状态成功"), HttpStatus.OK);
        } catch (Exception e) {
            log.error("修改用户状态异常：", e);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("修改状态失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return successEntity;
    }

    @Override
    public ResponseEntity<ResultInfo> systemOrganizationInfoByIdPost(String id) {
        ResponseEntity<ResultInfo> successEntity;
        SystemOrganizationInfo info = systemOrganizationService.findById(id);
        successEntity = new ResponseEntity<>(new ResultInfo().code(HttpStatus.OK.value()).message("成功").data(info), HttpStatus.OK);

        return successEntity;
    }

    @Override
    public ResponseEntity<ResultStatusList> statusListPost() {
        ResponseEntity<ResultStatusList> responseEntity;
        try {
            List<Status> data = systemOrganizationService.findAllStatus();
            responseEntity = new ResponseEntity<>(new ResultStatusList().code(HttpStatus.OK.value()).message("成功").data(data), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询状态列表异常：", e);
            responseEntity = new ResponseEntity<>(new ResultStatusList().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询状态列表失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }
}
